package com.central.apps.controller;


import com.central.app.controller.FlowCommonController;
import com.central.app.service.HandlerService;
import com.central.bizcommon.model.flow.FlowOpinion;
import io.swagger.annotations.Api;
import net.sf.json.JSONObject;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "流程审批")
@RequestMapping("/complete")
public class CompleteFlowController {
    @Autowired
    private FlowCommonController flowCommonController;
    @Autowired
    private HandlerService handlerService;

    @RequestMapping(value = "/flow", method = RequestMethod.POST)
    public String completeflow(HttpServletRequest request, @RequestBody Map<String, Object> params) throws Exception {
        JSONObject json = new JSONObject();
        try {
            String sql = "select * from  gq_conapplication where id = " + params.get("id");
            List<Map<String, Object>> list = handlerService.findBySql(sql);
            String createuser = MapUtils.getString(list.get(0),"createuser");
            String createuserdesc = MapUtils.getString(list.get(0),"createuserdesc");
            String deptcode = MapUtils.getString(list.get(0),"deptcode");
            int conid = 0;
            Map<String, Object> map = new HashMap<>();
            for (Map<String, Object> objectMap : list) {
                //通过插入合同台账
                map.put("concode", objectMap.get("concode"));//合同编号
                map.put("conname", objectMap.get("conname"));//合同名称
                map.put("contemplate", objectMap.get("contemplate"));//合同模板
                map.put("applicant", objectMap.get("applicant"));//申请人
                map.put("level", objectMap.get("level"));//级别
                map.put("deptcode", objectMap.get("deptcode"));//申请部门
                map.put("applytime", objectMap.get("applytime"));//申请日期
                map.put("protype", objectMap.get("protype"));//采购方式
                map.put("applycompany", objectMap.get("applycompany"));//申请单位
                map.put("conterm", objectMap.get("endtime"));//合同期限
                map.put("supplier", objectMap.get("supplier"));//供方名称
                map.put("outsourcingtype", objectMap.get("outsourcingtype"));//外协类型
                map.put("rationality", objectMap.get("rationality"));//外协主要内容及合理性
                map.put("conlevel", objectMap.get("conlevel"));//合同密级
                map.put("totalprice", objectMap.get("totalprice"));//合同金额（含税）
                map.put("objtype", objectMap.get("objtype"));//项目类型
                map.put("unitleader", objectMap.get("unitleader"));//单位领导
                map.put("title", objectMap.get("title"));//标题
                map.put("createuser", createuser);//创建人编号
                map.put("createuserdesc", createuserdesc);//创建人名称
                //map.put("deptcode", deptcode);//创建人部门
                //String sql1 = "select conid from gq_conaccount order by conid desc limit 1";
                //List<Map<String, Object>> aclist = handlerService.findBySql(sql1);
                //String conidd = (String) objectMap.get("conid");
                conid = Integer.valueOf(MapUtils.getString(objectMap, "conid"));
                map.put("conid", conid);//合同序号
                try {
                    String isreview = MapUtils.getString(objectMap,"isreview");
                    if("是".equals(isreview)){
                        map.put("isonline", objectMap.get("isonline"));//是否线上评审
                        map.put("firstname", objectMap.get("applycompanyEntity_fullname"));
                        map.put("reviewtype", objectMap.get("reviewtype"));//评审方式
                        map.put("ispublicity", objectMap.get("ispublicity"));//是否公示
                        map.put("publicity", objectMap.get("publicity"));//公示天数
                        map.put("secondname", objectMap.get("supplier"));//乙方名称
                        map.put("cosid", objectMap.get("cosid"));//
                        map.put("endtime", objectMap.get("endtime"));//合同结束时间
                        map.put("constatus", "评审中");
                        addconconreviews(map);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

//            List<Map<String, Object>> list1 = handlerService.findBySql("select id from  gq_conreviews where conid = " + params.get("conid"));
//            long id = MapUtils.getLongValue(list1.get(0), "id");
//            String app = "conreviewsApp";
//            FlowOpinion op = new FlowOpinion();
//            op.setOpinion("同意");
//            Map<String, Object> objmap = new HashMap<>();
//            objmap.put("direction", "1");
//            flowCommonController.completeFlow(app, id, op, objmap);
            //flowCommonController.completeFlow(app,id,op,objmap);

            json.put("msg", "发起评审成功");
            return json.toString();
        } catch (Exception e) {
            json.put("msg", "发起评审失败");
            e.printStackTrace();
        }
        return json.toString();
    }

    //合同申请完毕后，自动添加到合同评审中的会签环节
    public void addconconreviews(Map<String, Object> map ){
        try {
            handlerService.insert("gq_conreviews", map);
            //long id = 0l;

            List<Map<String, Object>> list = handlerService.findBySql("select id from  gq_conreviews where conid = "+map.get("conid"));
            long id = MapUtils.getLongValue(list.get(0),"id");
            System.out.println("===========主键id======="+list.get(0).get("id"));

            String app = "conreviewsApp";
            flowCommonController.startFlow(app,id);
            FlowOpinion op = new FlowOpinion();
            op.setOpinion("同意");
            Map<String,Object> objmap = new HashMap<>();
            objmap.put("direction","1");
            flowCommonController.completeFlow(app,id,op,objmap);
            flowCommonController.completeFlow(app,id,op,objmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
